Completed
Push — dev ( f66596...4953aa )
by Fike
32s
created

CancellationToken.js ➔ ... ➔ this.isCancelled   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
1
var Future = require('./Future').Future
2
var Slf4j = require('../logger').Slf4j
3
4
// TODO: use interfaces instead of direct cancellation token class references
5
6
/**
7
 * This class exists to propagate cancellation through different concurrent
8
 * branches.
9
 *
10
 * In many task end user will need to track several execution branches,
11
 * cancelling ones that did not win as soon as winner is found, e.g.
12
 * simultaneously waiting for callee to pick up and caller to hang up. To do
13
 * so, a cancellation token may be passed and activated, so parallel branch
14
 * may know if it's competitor has won.
15
 *
16
 * @see https://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.aspx
17
 *
18
 * @class
19
 *
20
 * @param {CancellationToken[]} [deps] Token dependencies
21
 * @param {string} [name] Optional token name
22
 */
23
function CancellationToken (deps, name) {
24
  var future = new Future()
25
  var flag = false
26
  var self = this
27
  var loggerName = 'ama-team.voxengine-sdk.concurrent.cancellation-token'
28
  var logger = Slf4j.create(loggerName)
29
  logger.attach('name', name)
30
31
  /**
32
   * @return {boolean}
33
   */
34
  this.isCancelled = function () { return flag }
35
36
  this.cancel = function () {
37
    logger.trace('Token has been cancelled')
38
    future.resolve()
39
    flag = true
40
  }
41
42
  /**
43
   * @function CancellationToken#then
44
   *
45
   * @param {Function}
46
   *
47
   * @return {Thenable}
48
   */
49
  this.then = future.then
50
51
  /**
52
   * Adds new dependency
53
   *
54
   * @param {CancellationToken} token
55
   */
56
  this.addDependency = function (token) { token !== self && token.then(self.cancel) }
57
58
  deps = deps || []
59
  deps.forEach(this.addDependency)
60
}
61
62
module.exports = {
63
  CancellationToken: CancellationToken
64
}
65